#include "playMediaUtils.h"

#include "watermask_cv_dct.h"
int  playMediaUtils::playstream_frameToBuf(char* yuvBuf, void* pframe)
{
	int pos1 = 0;
	/*AVFrame* tpframe = (AVFrame*)pframe;
	
	for (int i = 0; i < tpframe->height; i++) {
		memcpy(yuvBuf + pos1, tpframe->data[0] + i * tpframe->linesize[0], tpframe->width);
		pos1 += tpframe->width;
	}

	for (int i = 0; i < tpframe->height / 2; i++) {
		memcpy(yuvBuf + pos1, tpframe->data[1] + i * tpframe->linesize[1], tpframe->width / 2);
		pos1 += tpframe->width / 2;
	}

	for (int i = 0; i < tpframe->height / 2; i++) {
		memcpy(yuvBuf + pos1, tpframe->data[2] + i * tpframe->linesize[2], tpframe->width / 2);
		pos1 += tpframe->width / 2;
	}*/
	return pos1;
}
void playMediaUtils::playstream_bufToframe(int _w, int _h, char* _buf, void* pframe)
{
	/*int curpos = 0;
	AVFrame* tpframe = (AVFrame*)pframe;
	
	for (int i = 0; i < _h; i++) {
		memcpy(tpframe->data[0] + i * tpframe->linesize[0], _buf + curpos, _w);
		curpos += _w;
	}

	for (int i = 0; i < tpframe->height / 2; i++) {
		memcpy(tpframe->data[1] + i * tpframe->linesize[1], _buf + curpos, _w / 2);
		curpos += _w / 2;
	}

	for (int i = 0; i < tpframe->height / 2; i++) {
		memcpy(tpframe->data[2] + i * tpframe->linesize[2], _buf + curpos, _w / 2);
		curpos += _w / 2;
	}*/
}

bool g_bhas_logo_220521 = false;
int  g_logo_yuv_w_0307 = 128;  //yuv水印信息,水印固定格式
int  g_logo_yuv_h_0307 = 16;
int  g_logo_yuv_total = 0;
char* g_logo_yuv_0307 = NULL;
static watermask_cv_dct m_waterMaskObj;  //dct 水印
static cv_map_picture   g_mapPicture;  //贴图
void playMediaUtils::setWaterMarkWH(int _width, int _height)
{
	g_logo_yuv_w_0307 = _width;
	g_logo_yuv_h_0307 = _height;
}
bool playMediaUtils::playstream_hasLogo()
{
	return g_bhas_logo_220521;
}
void playMediaUtils::playstream_loadLogo()
{
	g_mapPicture.loadpic();

	//加载水印信息
	FILE* pfile = fopen("logo.yuv", "rb");
	//FILE* pfile = fopen("logo16.yuv", "rb");
	if (NULL != pfile) {
		/*char buf[6];
		memset(buf, 0, sizeof(buf));
		fread(buf, 1, sizeof(buf), pfile);
		g_logo_yuv_w_0307 = atoi(buf);
		fread(buf, 1, sizeof(buf), pfile);
		g_logo_yuv_h_0307 = atoi(buf);*/

		g_logo_yuv_total = g_logo_yuv_w_0307 * g_logo_yuv_h_0307;

		g_logo_yuv_0307 = new char[g_logo_yuv_total * 3 / 2];
		fread(g_logo_yuv_0307, 1, g_logo_yuv_total * 3 / 2, pfile);
		fclose(pfile);
		g_bhas_logo_220521 = true;
		printf("load logo.yuv success: width:%d,height:%d\n", g_logo_yuv_w_0307, g_logo_yuv_h_0307);
		return;
	}
	else {
		printf("logo file error: logo.yuv\n");
		//后面继续加载
	}
	
	//尝试加载水印
	m_waterMaskObj.loadWatermask("logo.bmp");
	g_bhas_logo_220521 = m_waterMaskObj.has();
}
void playMediaUtils::playstream_AddWarterMark(char* yuv, int yuv_width, int yuv_height)
{
	if (!g_bhas_logo_220521) {
		return;
	}
	//g_mapPicture.test(yuv, yuv_width, yuv_height); //贴图

	//添加水印
	if (g_logo_yuv_0307) {
		//拷贝yuv机制。 逐步废弃
		char* yuv_y = g_logo_yuv_0307;
		char* yuv_u = g_logo_yuv_0307 + g_logo_yuv_w_0307 * g_logo_yuv_h_0307;
		char* yuv_v = yuv_u + g_logo_yuv_w_0307 * g_logo_yuv_h_0307 / 4;

		memcpy(yuv, yuv_y, g_logo_yuv_w_0307 * g_logo_yuv_h_0307);
		memcpy(yuv + yuv_width * yuv_height, yuv_u, g_logo_yuv_w_0307 * g_logo_yuv_h_0307 / 4);
		memcpy(yuv + (yuv_width * yuv_height * 5 / 4), yuv_v, g_logo_yuv_w_0307 * g_logo_yuv_h_0307 / 4);
		return;  //后面不处理了
	}

	m_waterMaskObj.addImageWatermarY(yuv, yuv_width, yuv_height); 
}

bool playMediaUtils::GetWarterMarkYUV(char* yuv, int yuv_width, int yuv_height)
{

	if (g_logo_yuv_0307) {
		//读取yuv水印
		int wartermarkyuv_width = g_logo_yuv_w_0307;
		int wartermarkyuv_height = g_logo_yuv_h_0307;
		//读取图片
		char* pgetbuf = new char[wartermarkyuv_width * wartermarkyuv_height * 3 / 2];
		char* yuv_y = pgetbuf;
		char* yuv_u = pgetbuf + wartermarkyuv_width * wartermarkyuv_height;
		char* yuv_v = yuv_u + wartermarkyuv_width * wartermarkyuv_height / 4;

		memcpy(yuv_y, yuv, wartermarkyuv_width * wartermarkyuv_height);
		memcpy(yuv_u, yuv + yuv_width * yuv_height, wartermarkyuv_width * wartermarkyuv_height / 4);
		memcpy(yuv_v, yuv + (yuv_width * yuv_height * 5 / 4), wartermarkyuv_width * wartermarkyuv_height / 4);
		FILE* pf = fopen("logo_out.yuv", "wb");
		if (nullptr == pf) {
			return false;
		}
		fwrite(pgetbuf, 1, wartermarkyuv_width * wartermarkyuv_height * 3 / 2, pf);
		fclose(pf);
		return true;
	}

#if 1
	//获取dct水印. 必须制定水印的宽和高
	cv::Mat mainYUuvImage(yuv_height + yuv_height / 2, yuv_width, CV_8UC1, (unsigned char*)yuv);
	cv::Mat mainRgbImage;
	cv::cvtColor(mainYUuvImage, mainRgbImage, CV_YUV2BGR_I420);//CV_YUV420p2RGB

//	imshow("cv recv yuvaddwatermask", mainRgbImage);
	int watermask_w = g_logo_yuv_w_0307;
	int watermask_h = g_logo_yuv_h_0307;

	watermask_cv_dct testimg2;
	Mat watermarkImg = testimg2.getImageWatermarkWithText(mainRgbImage, watermask_w, watermask_h);

	//cv::resize(watermarkImg, watermarkImg, cv::Size(128, 128));
	//imshow("cv recv water", watermarkImg);

	cv::imwrite("logo_dct_out.png",watermarkImg);
	//cv::waitKey(0);

	Mat bgrwater;
	cv::cvtColor(watermarkImg, bgrwater, CV_GRAY2BGR);
	Mat YUVIMG(watermask_h + watermask_h / 2, watermask_w, CV_8UC1);  //构造yuv空间
	cv::cvtColor(bgrwater, YUVIMG, CV_BGR2YUV_I420);

	FILE* pfile = fopen("logo_dct_out.yuv", "wb");
	fwrite(YUVIMG.data, 1, watermask_w * watermask_h * 3 / 2, pfile);
	fclose(pfile);
#endif
	return true;
}